package shared;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(readCfgFile parseHosts completeDBParams hostName
		);
use strict;



#######################################################

sub readCfgFile
{
	my $file = shift;
   	my $pParm = shift;
    
   	open IF, "$file" or &shared::exit_w_error($_, "Failed to locate $file");
    while (<IF>) {
        my ($line) = split /\#/;
        next if (!$line);
        chomp $line;
        next if not $line =~ /=/;
        my ($param, $val) = split /\s*=\s*/, $line;
        $param =~ s/^\s+//;
        $val =~ s/^\s+//;
        $param =~ s/\s+$//;
        $val =~ s/\s+$//;
        $pParm->{$param} = ( $val ? $val : "");

    }
}



#############################################################

sub parseHosts
{
	my $file = shift;
    my $pSecs = shift;
    
    my %curSec;
    my %expected = (PAVE_host => 1, PAVE_user => 1, PAVE_password => 1,
    		DB_host => 1, DB_user => 1, DB_password => 1);
    
    open F, $file or die;
    
    while (<F>)
    {
        my ($line) = split /\#/;
        next if (!$line);
        chomp $line;
        next if not $line =~ /=/;
        my ($param, $val) = split /\s*=\s*/, $line;
        $param =~ s/\s+//g;
        $val =~ s/\s+//g;
        if (defined $expected{$param})
        {
        	$param =~ s/^DB_/PAVE_/;
			$curSec{$param} = $val;
            if (3 == scalar keys %curSec)
            {
            	my $host = $curSec{PAVE_host};
                foreach my $key (keys %expected)
                {
                	$pSecs->{$host}{$key} = $curSec{$key};
                }
                if (1 == scalar keys %$pSecs) # First section is the default
                {
                    foreach my $key (keys %expected)
                    {
                	    $pSecs->{"__DeFaUlT"}{$key} = $curSec{$key};
                    }
                
                }

                
                %curSec = ();
            }
		}            
    }
        
}

#############################################################

sub completeDBParams
{
	my $pParams = shift;
    my $HOSTScfg = shift;
    
    if ( (not defined $pParams->{PAVE_user}) || $pParams->{PAVE_user} eq ""  ||
		    (not defined $pParams->{PAVE_host}) || $pParams->{PAVE_host} eq ""  )
    {
	    #print "User/host not specified in PAVE.cfg.\n";
        if (-f $HOSTScfg)
        {
    	    #print "Looking in HOSTS.cfg\n";
            my %secs;
            parseHosts($HOSTScfg,\%secs);
            my $nEntry = scalar keys %secs;
            if (0) #$nEntry == 1)
            {
            	my $host = (keys %secs)[0];
                my $user = $secs{$host}{PAVE_user};
                my $pass = $secs{$host}{PAVE_password};
        	    print "Using host=$host,user=$user,password=$pass\n";
        	    $pParams->{PAVE_host} = $host;
        	    $pParams->{PAVE_user} = $user;
        	    $pParams->{PAVE_password} = $pass;
            }
            elsif (defined $pParams->{PAVE_host} && defined $secs{$pParams->{PAVE_host}})
           	{
            	my $host = $pParams->{PAVE_host};
                my $user = $secs{$host}{PAVE_user};
                my $pass = $secs{$host}{PAVE_password};
        	    print "Using host=localhost,user=$user,password=$pass\n";
        	    $pParams->{PAVE_user} = $user;
        	    $pParams->{PAVE_password} = $pass;            	
            }
            else
            {
            	#print "HOSTS.cfg has $nEntry entries.\n";
                my $host = hostName();
                if ($host eq "")
                {
                	print "Unable to get local hostname\n";
                    print "Enter full database information into PAVE.cfg and LIB.cfg\n";
                }
                else
                {
                	#print "Using local host $host\n";
                	if (defined $secs{$host})
                    {
                        my $user = $secs{$host}{PAVE_user};
                        my $pass = $secs{$host}{PAVE_password};
        	            #print "Using host=localhost,user=$user,password=$pass\n";
        	            $pParams->{PAVE_host} = "localhost";
        	            $pParams->{PAVE_user} = $user;
        	            $pParams->{PAVE_password} = $pass;
                    }
                	elsif (defined $secs{"__DeFaUlT"})
                    {
                        my $user = $secs{"__DeFaUlT"}{PAVE_user};
                        my $pass = $secs{"__DeFaUlT"}{PAVE_password};
        	            #print "Using host=localhost,user=$user,password=$pass\n";
        	            $pParams->{PAVE_host} = "localhost";
        	            $pParams->{PAVE_user} = $user;
        	            $pParams->{PAVE_password} = $pass;
                    }
                    
                    else
                    {
                    	print "Local host ($host) not found in HOSTS.cfg.\n ";
                        print "Specify database parameters in either LIB.cfg/PAVE.cfg, or HOSTS.cfg.\n";
                        exit(0);
                    }
                }
            }
        }
        else
        {
        	print "Can't find HOSTS.cfg. Specify database parameters in either LIB.cfg/PAVE.cfg, or HOSTS.cfg.\n";
    	    exit(0);
        }
    }
}    

#############################################################

sub hostName
{
	my $full = $ENV{HOSTNAME} || `uname -n`;
    return "" if not defined $full;
    #$full =~ s/\..*//;
    return $full; 
}
